﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FisApp.enums;
using FisApp.utility;
using System.Data.SqlClient;

namespace FisApp
{
    public partial class FrmKaliteKalip : Form
    {
        #region Variables...
        private string kaliteId;
        private string sqlCommandType = SqlCommandType.INSERT;

        public string kaliteNo
        {
            get
            {
                return txtKaliteNo.Text;
            }
            set
            {
                txtKaliteNo.Text = value;
            }
        }

        public string kalipNo
        {
            get
            {
                return txtKalipNo.Text;
            }
            set
            {
                txtKalipNo.Text = value;
            }
        }

        public DataTable Kaliplar
        {
            get
            {
                return (DataTable)gcKaliteKalip.DataSource;
            }

            set
            {
                gcKaliteKalip.DataSource = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                return gvKaliteKalip.GetDataRow(gvKaliteKalip.FocusedRowHandle);
            }
        }
        #endregion

        #region Consructor...
        public FrmKaliteKalip()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods...
        private void clearForm()
        {
            kaliteId = null;
            kaliteNo = null;
            kalipNo = null;
            btnVazgec.Visible = false;
            sqlCommandType = SqlCommandType.INSERT;
        }

        private void LoadKaliplar()
        {
            try
            {
                string query = "SELECT * FROM kalite";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                    Kaliplar = ds.Tables[0];
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }
        #endregion

        #region Events...
        private void FrmKaliteKalip_Load(object sender, EventArgs e)
        {
            LoadKaliplar();
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(kaliteNo))
            {
                FrmMessage.showDialog("Lütfen kalite no giriniz.", MessageTypes.Warning);
                return;
            }
            if (string.IsNullOrEmpty(kalipNo))
            {
                FrmMessage.showDialog("Lütfen kalıp no giriniz.", MessageTypes.Warning);
                return;
            }

            try
            {
                if(sqlCommandType.Equals(SqlCommandType.INSERT)){
                    string command = "INSERT INTO kalite (kalite_no, kalip_no) VALUES (@kalite_no, @kalip_no)";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@kalite_no", kaliteNo));
                    list.Add(DBUtils.GetParameter("@kalip_no", kalipNo));

                    DBUtils.ExecuteSQLCommand(command, list);
                } else if(sqlCommandType.Equals(SqlCommandType.UPDATE)){
                    string command = "UPDATE kalite SET kalite_no=@kalite_no, kalip_no=@kalip_no WHERE kalite_id=@kalite_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@kalite_no", kaliteNo));
                    list.Add(DBUtils.GetParameter("@kalip_no", kalipNo));
                    list.Add(DBUtils.GetParameter("@kalite_id", kaliteId));

                    DBUtils.ExecuteSQLCommand(command, list);
                }
                LoadKaliplar();
                clearForm();
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Kayıt işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void repbtnEdit_Click(object sender, EventArgs e)
        {
            kaliteId = SelectedRow["kalite_id"].ToString();
            kaliteNo = SelectedRow["kalite_no"].ToString();
            kalipNo = SelectedRow["kalip_no"].ToString();
            sqlCommandType = SqlCommandType.UPDATE;
            btnVazgec.Visible = true;
        }

        private void repbtnDelete_Click(object sender, EventArgs e)
        {
            kaliteId = SelectedRow["kalite_id"].ToString();
            
            try
            {
                if (FrmMessage.ShowDialogResult(SelectedRow["kalite_no"].ToString() + " no'lu kalite bilgisini silmek istediğinize emin misiniz ?", MessageTypes.Question) == DialogResult.Yes)
                {
                    string command = "DELETE FROM kalite WHERE kalite_id=@kalite_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@kalite_id", kaliteId));

                    DBUtils.ExecuteSQLCommand(command, list);

                    LoadKaliplar();
                    clearForm();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().Contains("-2146232060"))
                    FrmMessage.showDialog(General.ForeignKeyExceptionMessage, MessageTypes.Error);
                else
                    FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);

                //FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void btnVazgec_Click(object sender, EventArgs e)
        {
            clearForm();
        }

        private void FrmKaliteKalip_Activated(object sender, EventArgs e)
        {
            FrmKaliteKalip_Load(null, null);
        }
        #endregion
    }
}
